// 引入必要的命名空间
using UniversalAdmin.Application.DTOs; // 引入数据传输对象定义
using UniversalAdmin.Domain.Entities; // 引入领域实体定义
using UniversalAdmin.Domain.Repositories; // 引入仓储接口定义

// 定义服务层的命名空间
namespace UniversalAdmin.Application.Services;

// 实现操作日志服务接口
public class OperationLogService : IOperationLogService
{
    // 注入操作日志仓储接口
    private readonly IOperationLogRepository _logRepository;

    // 构造函数，依赖注入操作日志仓储接口
    public OperationLogService(IOperationLogRepository logRepository)
    {
        _logRepository = logRepository;
    }

    // 分页获取操作日志列表
    public async Task<PagedResultDto<OperationLogDto>> GetLogsAsync(int page = 1, int pageSize = 10, string? userId = null, string? operation = null, DateTime? startDate = null, DateTime? endDate = null)
    {
        // 调用仓储层获取分页日志列表和总数
        var (logs, totalCount) = await _logRepository.GetPagedAsync(page, pageSize, userId, operation, startDate, endDate);
        // 将日志实体映射为DTO
        var logDtos = logs.Select(MapToDto);
        
        // 返回分页结果
        return new PagedResultDto<OperationLogDto>
        {
            Items = logDtos.ToList(),
            TotalCount = totalCount,
            Page = page,
            PageSize = pageSize
        };
    }

    // 根据ID获取单个操作日志
    public async Task<OperationLogDto?> GetLogByIdAsync(Guid id)
    {
        // 调用仓储层根据ID获取日志
        var log = await _logRepository.GetByIdAsync(id);
        // 如果日志存在，映射为DTO并返回；否则返回null
        return log != null ? MapToDto(log) : null;
    }

    // 创建新操作日志
    public async Task<OperationLogDto> CreateLogAsync(CreateOperationLogDto dto)
    {
        // 根据DTO创建日志实体
        var log = new OperationLog
        {
            UserId = dto.UserId,
            UserName = dto.UserName,
            Operation = dto.Operation,
            Description = dto.Description,
            IpAddress = dto.IpAddress,
            UserAgent = dto.UserAgent,
            CreatedAt = DateTime.UtcNow
        };

        // 调用仓储层添加日志
        var createdLog = await _logRepository.AddAsync(log);
        // 将新创建的日志映射为DTO并返回
        return MapToDto(createdLog);
    }

    // 根据用户ID获取操作日志列表
    public async Task<IEnumerable<OperationLogDto>> GetLogsByUserAsync(Guid userId, int page = 1, int pageSize = 10)
    {
        // 调用仓储层根据用户ID获取日志列表
        var logs = await _logRepository.GetByUserAsync(userId, page, pageSize);
        // 将日志实体映射为DTO并返回
        return logs.Select(MapToDto);
    }

    // 根据操作类型获取操作日志列表
    public async Task<IEnumerable<OperationLogDto>> GetLogsByOperationAsync(string operation, int page = 1, int pageSize = 10)
    {
        // 调用仓储层根据操作类型获取日志列表
        var logs = await _logRepository.GetByOperationAsync(operation, page, pageSize);
        // 将日志实体映射为DTO并返回
        return logs.Select(MapToDto);
    }

    // 根据日期范围获取操作日志列表
    public async Task<IEnumerable<OperationLogDto>> GetLogsByDateRangeAsync(DateTime startDate, DateTime endDate, int page = 1, int pageSize = 10)
    {
        // 调用仓储层根据日期范围获取日志列表
        var logs = await _logRepository.GetByDateRangeAsync(startDate, endDate, page, pageSize);
        // 将日志实体映射为DTO并返回
        return logs.Select(MapToDto);
    }

    // 将操作日志实体映射为DTO
    private static OperationLogDto MapToDto(OperationLog log)
    {
        return new OperationLogDto
        {
            Id = log.Id,
            UserId = log.UserId,
            UserName = log.UserName,
            Operation = log.Operation,
            Description = log.Description,
            IpAddress = log.IpAddress,
            UserAgent = log.UserAgent,
            CreatedAt = log.CreatedAt
        };
    }
} 